#include "MissileBody.h"

/*----------------------------------------------------------------------------*/
void MissileBody::missileInit(int ID, Missile* mInfo, Vec3f loc, Vec3f dest) {

	myInfo=mInfo;
	destination = dest;
	lastPos = loc;
	Vec3f temp = destination - lastPos;
	distance = temp.length();
	orgDist = distance;
	onTime = 300.0f;
	exploded = false;
	Vec3f vel = temp;
	vel.normalize();
	vel /= 20;
	init(ID, loc, vel, .01f, 0.0001f);	

}

/*----------------------------------------------------------------------------*/
void MissileBody::explodeMissile() {

	fMass = myInfo->getPower();
	vVel = Vec3f(0.0f, 0.0f, 0.0f);
	exploded = true;
}

/*----------------------------------------------------------------------------*/
const Vec3f* MissileBody::getDestination() { return &destination; }
float MissileBody::getProgress() { return progress; }
float MissileBody::getOnTime() { return onTime; }
const Vec3f* MissileBody::getMLoc() { return &vLoc; }
float MissileBody::getMMass() { return fMass; }
bool MissileBody::isExploded() { return exploded; }

/*----------------------------------------------------------------------------*/
void MissileBody::updatePosition(float timeStep) {

	updateWithForce(Vec3f(0,0,0), timeStep);

	Vec3f temp = vLoc - lastPos;	
	distance -= temp.length();

	//check to see if we are there yet
	if (distance <= 0)
		progress = 1.0f;
	else
		progress = 1-(orgDist/distance);
	
	//if we already exploded then start decrementing time
	if (exploded) {
		onTime -= timeStep;
		
		//if time is <= 0 then make mass 0
		if (onTime <= 0)
			fMass = 0;
	}

	lastPos = vLoc;
}

void MissileBody::updateMissileHistory(float timeStep) {
	updateHistory(timeStep);
}